package com.xszx.controller;
import com.github.pagehelper.PageInfo;
import com.xszx.config.PageRequest;
import com.xszx.entity.*;
import com.xszx.service.WarehouseService;
import com.xszx.vo.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/warehouseList")
    public AjaxJson WarehouseList(PageRequest pageRequest,
                            @RequestParam(defaultValue = "1") int page,    // 当前页码
                            @RequestParam(defaultValue = "10") int size) {
        pageRequest.setPage(page);
        pageRequest.setSize(size);
//        System.out.println(pageRequest);
        AjaxJson j = new AjaxJson();
        try {
            PageInfo<Warehouse> pageInfo = warehouseService.WarehouseList(pageRequest);
            j.put("wareHouseList", pageInfo.getList());
            j.put("total", pageInfo.getTotal());
        } catch (Exception e) {
            j.setErrMsg(e.getMessage());
            j.setSuccess("false");
            e.printStackTrace();
        }
        return j;
    }
    //获取仓库详细信息(区域信息)
    @RequestMapping("/getWarehouseRegionInfoByNumber/{number}")
    private AjaxJson GetWarehouseRegionInfoByNumber(@PathVariable("number") String number){
        AjaxJson ajaxJson = new AjaxJson();
        List<WarehouseRegionInfo> warehouseInfos = warehouseService.GetWarehouseRegionInfoByNumber(number);
        System.out.println(warehouseInfos);
        ajaxJson.put("warehouseInfos",warehouseInfos);
        if(warehouseInfos==null){
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("获取失败");
        }
        return ajaxJson;
    }
    //删除仓库根据仓库编号(仓库有货物的时候不能删除，删除的时候还需要删除所有区域)
    @RequestMapping("/delWarehouseByNumber/{number}")
    private AjaxJson DelWarehouseByNumber(@PathVariable("number") String number){
        AjaxJson ajaxJson = new AjaxJson();
        boolean flag = warehouseService.delWarehouseByNumber(number);
        String msg = flag==true?"删除成功":"删除失败，仓库有货物";
        ajaxJson.put("msg",msg);
        return ajaxJson;
    }
    //增加仓库
    @RequestMapping("/addWarehouse")
    private AjaxJson AddWarehouse(@RequestBody Warehouse warehouse) {
        AjaxJson ajaxJson = new AjaxJson();
        String msg = (warehouseService.addWarehouse(warehouse))==true?"增加成功":"增加失败";
        ajaxJson.put("msg",msg);
        return ajaxJson;
    }
    //回显仓库类型(增加仓库的时候回显类型)
    @RequestMapping("/getWarehouseType")
    private AjaxJson GetWarehouseType() {
        AjaxJson ajaxJson = new AjaxJson();
        List<Type> typeList = warehouseService.getWarehouseType();
        ajaxJson.put("typeList",typeList);
        return ajaxJson;
    }

    //回显仓库的信息，通过仓库的编号（修改的时候可以用）
    @RequestMapping("/getWarehouseInfoByNumber")
    private AjaxJson GetWarehouseInfo(String number){
        AjaxJson ajaxJson = new AjaxJson();
        Warehouse warehouse = warehouseService.getWarehouseByNumber(number);
        ajaxJson.put("warehouse",warehouse);
        return ajaxJson;
    }

    //获取区域的类型（增加仓库的区域的时候使用）
    @RequestMapping("/getRegionType")
    private AjaxJson getWarehouseType(){
        AjaxJson ajaxJson = new AjaxJson();
        List<Type> typeList = warehouseService.getRegionType();
        ajaxJson.put("typeList",typeList);
        return ajaxJson;
    }

    //给仓库添加区域
    @RequestMapping("/addRegionByWarehouseNumber")
    private AjaxJson addRegionByWarehouseNumber(@RequestBody WarehouseRegionInfo warehouseRegionInfo){
        System.out.println(warehouseRegionInfo);
        AjaxJson ajaxJson = new AjaxJson();
        boolean flag = warehouseService.addRegionByWarehouseNumber(warehouseRegionInfo);
        String msg = flag ==true?"添加成功":"添加失败";
        ajaxJson.put("msg",msg);
        return ajaxJson;
    }

    //修改区域信息
    @RequestMapping("/updateRegion")
    public  AjaxJson updateRegion(@RequestBody WarehouseRegionInfo warehouseRegionInfo){
        AjaxJson a = new AjaxJson();
        try {
            warehouseService.updateRegion(warehouseRegionInfo);
            a.setErrMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            a.setSuccess("false");
            a.setErrMsg("修改失败"+e.getMessage());
        }
        return a;
    }


    //通过区域的number删除区域(必须区域内没有放置东西才可以删除)
    @RequestMapping("/delRegion/{rid}")
    private AjaxJson delRegion(@PathVariable("rid") Integer rid){
        System.out.println(rid);
        AjaxJson j = new AjaxJson();
        try {
            boolean flag = warehouseService.delRegion(rid);
            if (flag) {
                j.setCode("200");  // 明确设置状态码
                j.setSuccess("true"); // 使用boolean值而不是字符串
                j.setErrMsg("删除成功");
                return j;
            } else {
                j.setCode("500");
                j.setSuccess("flase");
                j.setErrMsg("该区域仍有未出库的货物，无法删除");
                return j;
            }
        } catch (Exception e) {
            j.setCode("500");
            j.setSuccess("flase");
            j.setErrMsg("删除失败：" + e.getMessage());
            e.printStackTrace();
            return j;
        }
    }

    //修改仓库信息
    @RequestMapping("/updateWarehouse")
    public  AjaxJson updateUser(@RequestBody Warehouse warehouse){
        System.out.println(warehouse);
        AjaxJson a = new AjaxJson();
        try {
            warehouseService.updateWarehouse(warehouse);
            a.setErrMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            a.setSuccess("false");
            a.setErrMsg("修改失败"+e.getMessage());
        }
        return a;
    }

    //获取仓库的使用率
    @RequestMapping("/getWarehouseRate")
    public AjaxJson getWarehousesByType(){
        AjaxJson j = new AjaxJson();
        double turnoverRate =  warehouseService.getWarehousearea();
        System.out.println(turnoverRate);
        double turnoverTrend = warehouseService.getWarehouseGrend();
        System.out.println("增长率"+turnoverTrend);
        j.put("turnoverRate",turnoverRate);
        j.put("turnoverTrend",turnoverTrend);
        return j;
    }

    //获取仓位列表
    @RequestMapping("/getPositionList/{rid}")
    public AjaxJson getPositionList(@PathVariable("rid") Integer rid ){
        System.out.println(rid);
        AjaxJson ajaxJson = new AjaxJson();
        List<Position> PositionList = warehouseService.getpositionList(rid);
        System.out.println(PositionList);
        ajaxJson.put("PositionList",PositionList);
        ajaxJson.setSuccess("true");
        return ajaxJson;
    }

//    增加仓位
    @RequestMapping("/addPosition")
    public AjaxJson addPosition(@RequestBody Position position){
        //前端接受回来rid,location,area,space,state
        System.out.println(position);
        AjaxJson ajaxJson = new AjaxJson();
        warehouseService.addPosition(position);
        ajaxJson.put("msg","添加成功");
        return ajaxJson;
    }

    //修改仓位
    @RequestMapping("updatePosition")
    public AjaxJson updatePosition(@RequestBody Position position){
        System.out.println(position);
        AjaxJson ajaxJson = new AjaxJson();
        warehouseService.updatePosition(position);
        ajaxJson.put("msg","修改成功");
        return ajaxJson;
    }

    //删除仓位
    @RequestMapping(value = "/deletePosition/{id}", method = RequestMethod.DELETE)
    public AjaxJson delPosition(@PathVariable Integer id) {
        System.out.println("id"+id);
        AjaxJson ajaxJson = new AjaxJson();
        warehouseService.deletePosition(id);
        ajaxJson.put("msg","删除成功");
        return ajaxJson;
    }



}
